package com.example.mvplibrary.mvplibrary.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

public class BaseViewHolder extends RecyclerView.ViewHolder {

    private View rootView;
    private SparseArray<View> sparseArray;
    private Context context;

    public void setContext(Context context) {
        this.context = context;
    }

    public BaseViewHolder(@NonNull View itemView) {
        super(itemView);
        rootView=itemView;
        sparseArray=new SparseArray<>();
    }

    //找控件
    public <T extends View> T getView(int viewId){
        if (sparseArray.get(viewId)==null){
            View viewById = rootView.findViewById(viewId);
            sparseArray.put(viewId,viewById);
        }
        return (T) sparseArray.get(viewId);
    }

    public void setText(int textViewId,String text){
        TextView textView = getView(textViewId);
        textView.setText(text);
    }

    public void setImage(int ImageViewId,String imageUrl){
        ImageView imageView = getView(ImageViewId);
        Glide.with(context).load(imageUrl).apply(new RequestOptions().circleCrop()).load(imageView);
    }
}
